var rightwin = require('../../../template/rightwin/rightwin.js');
var app = getApp();

var rightIcons = {
  "EFF-30100":"money",
  "EFF-30200":"dollar",
  "EFF-30300": "vip",
  "EFF-30400": "coupon",
  "EFF-30500": "gift",
  "EFF-30600": "cafe",
  "EFF-30700": "memory",
  "EFF-30800": "more"
};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    role: '0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    rightwin.init(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.updateInfo();
  },
  updateInfo:function(){
    var rights = app.globalData.vipInfo.assets.LoyaltyCard[0].effects;
    for(var i=0;i<rights.length;i++){
      if (rights[i].enabled == true){
        rights[i].iconsrc = '../../imgs/my/rights/' + rightIcons[rights[i].key] + '1.png';
      }
      else{
        rights[i].iconsrc = '../../imgs/my/rights/' + rightIcons[rights[i].key] + '2.png';
      }
    }
    this.setData({
      role: app.globalData.vipInfo.userType,
      roleName: app.globalData.vipInfo.userTitle,
      bonusPoint: app.globalData.vipInfo.bonusPoint,
      rights: rights
    });
    if (this.data.role = 'vip') {
      var endtime = app.formatDateTime(app.globalData.vipInfo.assets.LoyaltyCard[0].validEnd * 0.001, false);
      this.setData({
        endtime: endtime
      });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  dealVip:function(){
    wx.showLoading({
      title: '',
    });
    if(this.data.roleName == '金麦会员'){
      this.renewVip();
    }
    else{
      this.openVip();
    }
  },
  openVip: function () {
    var url = '/yuanmai_we/silo/card/create/' + app.globalData.vipInfo.assets.LoyaltyCard[0].config;
    app.loadDataFromServer(url, this.openVipCallBack, {}, 'post', true);
  },
  openVipCallBack:function(res){
    console.log(res);
    wx.hideLoading();
    if (res.status == true) {
      wx.navigateTo({
        url: './buyvip/buyvip?payinfo=' + res.payInfo,
      });
    }
  },
  vipRightsDetail: function () {
    wx.navigateTo({
      url: './rightsdetail/rightsdetail',
    });
  },
  tryVip: function () {
    var url = '/silo/h5/2205.json';
    var params = {
      auth: app.globalData.authToken,
      dropKey: 'drop-trying-loyalty-card'
    };
    app.loadDataFromServer(url, this.tryVipCallBack, JSON.stringify(params), 'post');
  },
  tryVipCallBack: function (res) {
    console.log(res);
    if (res.protocError == 0){
      app.getVipInfo();
      var that = this;
      var delayid = setTimeout(function(){
        clearTimeout(delayid);
        that.updateInfo();
      },500);
    }
  },
  renewVip: function () {
    var url = '/yuanmai_we/silo/card/extend/' + app.globalData.vipInfo.assets.LoyaltyCard[0].config + '/' + app.globalData.vipInfo.assets.LoyaltyCard[0].assetId;
    app.loadDataFromServer(url, this.renewVipCallBack, {}, 'post', true);
  },
  renewVipCallBack: function (res) {
    console.log(res);
    wx.hideLoading();
    if (res.status == true){
      wx.navigateTo({
        url: './buyvip/buyvip?payinfo=' + res.payInfo,
      });
    }
  },
  //--------------------------------------
  closeRightWindow: rightwin.close,
  showRightwin: rightwin.show
})